package cc.rengu.igas.bomp.common.dao.impl;

import cc.rengu.igas.bomp.common.dao.ProdStageInfoMapper;
import cc.rengu.igas.bomp.common.entity.ProdStageInfo;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.util.DateUtil;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by 王成 on 2020/3/2
 */
public class ProdStageInfoMapperImpl implements ProdStageInfoMapper {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.MCMP_POOL_NAME);

    @Override
    public int insertProdStageInfo(ProdStageInfo prodStageInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "insertProdStageInfo");
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, prodStageInfo, null);
        if (0 != iReturnCode) {
            rglog.error("登记产品分期信息失败,instId:<{}>,prodType:<{}>,id:<{}>,iReturnCode:<{}>", prodStageInfo.getInstId(), prodStageInfo.getProdType(), prodStageInfo.getId(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public int updateProdStageInfoById(ProdStageInfo prodStageInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateProdStageInfoById");
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, prodStageInfo, null);
        if (0 != iReturnCode) {
            rglog.error("登记产品分期信息失败,instId:<{}>,prodType:<{}>,id:<{}>,iReturnCode:<{}>", prodStageInfo.getInstId(), prodStageInfo.getProdType(), prodStageInfo.getId(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public ProdStageInfo selectProdStageInfoById(String instId, String id) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectProdStageInfoById");
        ProdStageInfo prodStageInfo = new ProdStageInfo();
        prodStageInfo.setInstId(instId);
        prodStageInfo.setId(id);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, prodStageInfo, prodStageInfo);
        if (0 != iReturnCode) {
            rglog.error("获取产品分期信息失败,instId:<{}>,id:<{}>,iReturnCode:<{}>", instId, id, iReturnCode);
            return null;
        }
        return prodStageInfo;
    }

    @Override
    public ProdStageInfo selectProdStageInfoByPrimaryKey(String instId, String prodType, String baseProdCode, String prodStageCode, int prodStageNum) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectProdStageInfoByPrimaryKey");
        ProdStageInfo prodStageInfo = new ProdStageInfo();
        prodStageInfo.setInstId(instId);
        prodStageInfo.setProdType(prodType);
        prodStageInfo.setBaseProdCode(baseProdCode);
        prodStageInfo.setProdStageCode(prodStageCode);
        prodStageInfo.setProdStageNum(prodStageNum);
        prodStageInfo.setFeeEffectiveDate(DateUtil.getCurrentDate());
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, prodStageInfo, prodStageInfo);
        if (0 != iReturnCode) {
            rglog.error("获取产品分期信息失败,instId:<{}>,prodType:<{}>,baseProdCode:<{}>,prodStageCode:<{}>,prodStageNum:<{}>,iReturnCode:<{}>", instId, prodType, baseProdCode, prodStageCode, prodStageNum, iReturnCode);
            return null;
        }
        return prodStageInfo;
    }

    @Override
    public List<ProdStageInfo> selectProdStageInfoByProdType(String instId, String prodType, String baseProdCode) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectProdStageInfoByProdType");
        ProdStageInfo prodStageInfo = new ProdStageInfo();
        prodStageInfo.setInstId(instId);
        prodStageInfo.setProdType(prodType);
        prodStageInfo.setBaseProdCode(baseProdCode);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, prodStageInfo, ProdStageInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("获取产品分期信息失败,instId:<{}>,prodType:<{}>,baseProdCode:<{}>", instId, prodType, baseProdCode);
            return null;
        }
        return objectList.stream().map(item -> (ProdStageInfo) item).collect(Collectors.toList());
    }

    @Override
    public List<ProdStageInfo> selectProdStageInfoByProdStageCode(String instId, String prodType, String prodStageCode) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectProdStageInfoByProdStageCode");
        ProdStageInfo prodStageInfo = new ProdStageInfo();
        prodStageInfo.setInstId(instId);
        prodStageInfo.setProdType(prodType);
        prodStageInfo.setProdStageCode(prodStageCode);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, prodStageInfo, ProdStageInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("获取产品分期信息失败,instId:<{}>,prodType:<{}>,prodStageCode:<{}>", instId, prodType, prodStageCode);
            return null;
        }
        return objectList.stream().map(item -> (ProdStageInfo) item).collect(Collectors.toList());
    }

    @Override
    public List<ProdStageInfo> selectProdStageInfoByProdCode(String instId, String prodType, String baseProdCode, String prodCode) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectProdStageInfoByProdCode");
        ProdStageInfo prodStageInfo = new ProdStageInfo();
        prodStageInfo.setInstId(instId);
        prodStageInfo.setProdType(prodType);
        prodStageInfo.setBaseProdCode(baseProdCode);
        prodStageInfo.setProdCode(prodCode);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, prodStageInfo, ProdStageInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("获取产品分期信息失败,instId:<{}>,prodType:<{}>,baseProdCode:<{}>", instId, prodType, baseProdCode);
            return null;
        }
        return objectList.stream().map(item -> (ProdStageInfo) item).collect(Collectors.toList());
    }
}
